package cn.jia.hrm.exception;

import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AssertGlobal {

    //手机号的正则表达式
    public static final String REGEX_PHONE = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(16[5,6])|(17[0-8])|(18[0-9])|(19[1、5、8、9]))\\d{8}$";
    //邮箱正则表达式
    public static final String REGEX_EMAIL = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";

    //判断传过来的值是否为空，为空就抛异常  ，异常来自于后端service定义好了的
    public static void hasText(@Nullable String text, String message) {
        if (!StringUtils.hasText(text)) {
            throw new GlobalException(message);
        }
    }
    //Pattern :java 提供一个负责做正则表达式的一个类   Matcher：匹配的意思
    public static void isMobile(String mobile, String message) {
        Pattern pp = Pattern.compile(REGEX_PHONE, Pattern.CASE_INSENSITIVE);  //Pattern.CASE_INSENSITIVE  常量可以写可以不写
        Matcher m = pp.matcher(mobile);  //匹配 不满足条件就抛异常
        if(!m.matches()){
            throw new GlobalException(message);
        }
    }
    //邮箱断言
    public static void isEmail(String email, String message) {
        Pattern p = Pattern.compile(REGEX_EMAIL, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(email);
        if(!m.matches()){
            throw new GlobalException(message);
        }
    }
    //判断是否为空
    public static void isnull(Object obj, String message) {
        if (obj==null){
            throw  new GlobalException(message);
        }
    }
 //判断是否上线
    public static void isequls(Integer status, Integer i, String message) {
        if (status != i){
            throw  new GlobalException(message);
        }
    }
    public static void isEquals(String i1, String i2, String message) {
        hasText(i1 , message);
        hasText(i2 , message);
        if(!i1.equalsIgnoreCase(i2)){
            throw new GlobalException(message);
        }
    }
}
